<template>
	<view>
		<view @tap.stop.prevent @touchmove.stop.prevent>
			<wyb-loading ref="loading" type="square" loading-type="scale-line" animation="fade" :title="curProgressTitle" />
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	import wybLoading from "../wyb-loading/wyb-loading.vue"
	export default {
		name:"versionCheckTool",
		components: {
			wybLoading
		},
		props: {
		    checkState:{
		        type:String,
		        default:''
		    }
		},
		data() {
			return {
				curCheckState: "",
				curProgressTitle: "正在下载更新文件, 请稍后",
				curProgressNum:0,
			}
		},
		onLoad() {
		
		},
		onShow() {
		
		},
		created() {
			this.curCheckState = this.$props.checkState;
		},
		methods: {
			initViewData(param){
				this.curCheckState = param.checkState;
				this.curProgressNum = 0;
				this.getCheckVersionInfo();
		
			},
			//  检查更新 开始方法
			getCheckVersionInfo() {
		
				var _this = this;
				// 获取设备类型
				var devInfo = uni.getSystemInfoSync().platform;
				// android检查更新
				if (devInfo == "android") {
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						var urlBanBen = "https://www.pgyer.com/apiv2/app/check";
						uni.request({
							url: urlBanBen,
							method: "GET",
							data: {
								"_api_key": "29fcb9a62af67152be1b25024fd8183d",
								"appKey": "1b3f83a0440c3f8793b77b78a9fcfd0e",
								"buildVersion": wgtinfo.version
							},
						}).then(resData1 => {
							var pgyInfo = resData1[1].data.data;
							if (pgyInfo.buildHaveNewVersion == true) {
								_this.AndroidCheckUpdate();
							} else {
								console.log("------------zhoiule")
								if (this.curCheckState != 'auto') {
									// _this.$refs.uToast.show({
									// 	title: "当前版本已经是最新版本！",
									// 	position: "bottom"
									// })
									_this.$refs.uToast.show({
										type: "info",
										icon: true,
										position: "bottom",
										message: "当前版本已经是最新版本！",
									})
								}
		
							}
		
						})
		
		
					})
		
				} else if (devInfo == "ios") {
					if (this.curCheckState != 'auto') {
						let appleId = 1545804784
						plus.runtime.launchApplication({
							action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
						}, function(e) {
							console.log('Open system default browser failed: ' + e.message);
						});
					}
				}
		
		
			},
			AndroidCheckUpdate: function() {
				var _this = this;
				plus.nativeUI.confirm("检测到有新版本，是否更新", function(e) {
					if (e.index == 0) {
						if (plus.networkinfo.getCurrentType() != 3) {
							plus.nativeUI.confirm("检测到您目前非Wifi连接，是否继续更新", function(e) {
								if (e.index == 0) {
									_this.downWgt();
								} else {
		
								}
							}, "", )["取消", "确定"]
							return;
						}else{
							_this.downWgt(); //下载文件
						}
						
					} else {}
				}, "", ["立即更新", "以后再说"]);
			},
			
			// 下载
			downWgt: function() {
				var _this = this;
				console.log("-------------345345----1");
				_this.curProgressNum = 0;
				_this.$refs.loading.showLoading(); // 显示
				const downloadTask = uni.downloadFile({
					url: 'https://www.pgyer.com/apiv2/app/install?_api_key=29fcb9a62af67152be1b25024fd8183d&appKey=1b3f83a0440c3f8793b77b78a9fcfd0e', 
					//仅为示例，并非真实的资源
					success: (resData2) => {
						if (resData2.statusCode === 200) {
							console.log('下载成功');
							_this.$refs.loading.hideLoading() // 隐藏
							plus.runtime.install(resData2.tempFilePath); // 安装下载的apk文件
						} else {
							_this.$refs.loading.hideLoading() // 隐藏
							console.log('下载失败');
						}
					}
				});
			
				downloadTask.onProgressUpdate((resData3) => {
					this.curProgressNum = resData3.progress
					this.curProgressTitle = "当前更新进度" + this.curProgressNum + "%, 请稍后..."
			
				});
			
			},
		}
	}
</script>

<style lang="scss" scoped>
	
	::v-deep(.wyb-loading-title) {
		font-size: 12px !important;
	}

</style>
